package com.alibaba.dubbo.demo.provider;

public class DoubleLink {

    Node head;
    Node tail;
    int size;

    void addTail(Object o) {
        Node newNode = new Node(o);
        if (size == 0) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            newNode.prve = tail;
            tail = newNode;
        }
        size++;
    }

    void addHead(Object o) {
        Node newNode = new Node(o);
        if (size == 0) {
            head = newNode;
            tail = newNode;
        } else {
            head.prve = newNode;
            newNode.next = head;
            head = newNode;
        }
        size++;
    }

    Node delHead() {
        if (size == 0) {
            return null;
        }
        Node temp = null;
        if (size == 1) {
            temp = head;
            head = null;
            tail = null;
        } else {
            temp = head;
            head = temp.next;
            head.prve = null;
            temp.next = null;
        }
        size--;
        return temp;
    }

    class Node {
        Node prve;
        Node next;
        Object o;

        Node(Object o) {
            this.o = o;
        }
    }

    int size() {
        return size;
    }

    public static void main(String[] args) {
        DoubleLink doubleLink = new DoubleLink();
        doubleLink.addTail("1");
        doubleLink.addTail("2");
        doubleLink.addTail("3");
        doubleLink.addHead("0");

        doubleLink.addTail("4");

        Node node = doubleLink.head;
        for (; ; ) {
            if (node != null) {
                System.out.print(node.o);
            } else {
                break;
            }
            node = node.next;
        }
        System.out.println("  size :" + doubleLink.size);

        node = doubleLink.tail;
        for (; ; ) {
            if (node != null) {
                System.out.print(node.o);
            } else {
                break;
            }
            node = node.prve;
        }

        System.out.println("  size :" + doubleLink.size);

        doubleLink.delHead();
        doubleLink.delHead();

        node = doubleLink.head;
        for (; ; ) {
            if (node != null) {
                System.out.print(node.o);
            } else {
                break;
            }
            node = node.next;
        }

        System.out.println("  size :" + doubleLink.size);
        node = doubleLink.tail;
        for (; ; ) {
            if (node != null) {
                System.out.print(node.o);
            } else {
                break;
            }
            node = node.prve;
        }


        System.out.println("  size :" + doubleLink.size);

        doubleLink.delHead();
        doubleLink.delHead();
        doubleLink.delHead();
        doubleLink.delHead();
        node = doubleLink.tail;
        for (; ; ) {
            if (node != null) {
                System.out.print(node.o);
            } else {
                break;
            }
            node = node.prve;
        }
        System.out.println("  size :" + doubleLink.size);


    }
}
